package com.eastfair.builder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.builder.dao.ReportSiteMapper;
import com.eastfair.builder.dto.ExhibitorDTO;
import com.eastfair.builder.dto.ReportSiteDTO;
import com.eastfair.builder.entity.ReportSite;
import com.eastfair.builder.entity.ReportSpace;
import com.eastfair.builder.exceptioncode.BuilderExceptionCode;
import com.eastfair.builder.service.ExhibitorService;
import com.eastfair.builder.service.ReportSiteService;
import com.eastfair.builder.service.ReportSpaceService;
import com.eastfair.builder.vo.ReportSiteVO;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.exception.NoBackBizException;
import com.eastfair.home.dto.ReportSiteAuditDTO;
import com.eastfair.venueservice.consts.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 报馆展位基本信息
 * </p>
 *
 * @author dq
 * @date 2022-08-06
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportSiteServiceImpl extends SuperServiceImpl<ReportSiteMapper, ReportSite> implements ReportSiteService {

    @Resource
    private ExhibitorService exhibitorService;

    @Resource
    private ReportSpaceService reportSpaceService;

    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportSite> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportSite model) {
        if (getVOByReportSpaceId(model.getReportSpaceId()) != null) {
            throw NoBackBizException.wrap(BuilderExceptionCode.REPORT_SITE_HAS_EXIST);
        }
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public ReportSiteVO getVOByReportSpaceId(Long reportSpaceId) {
        log.info("getReportSiteVOByReportSpaceId - 报馆ID查询展位基本信息, reportSpaceId={}", reportSpaceId);
        ReportSite reportSite = getByReportSpaceId(reportSpaceId);
        if (reportSite == null) {
            return null;
        }
        return BeanUtil.toBean(reportSite, ReportSiteVO.class);
    }

    @Override
    public ReportSite getReportSite(ReportSiteDTO query) {
        log.info("getReportSite - 获取展位基本信息, query={}", query);
        QueryWrapper<ReportSite> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .last("limit 1")
                .eq(ReportSite::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getReportSpaceId() != null, ReportSite::getReportSpaceId, query.getReportSpaceId())
        ;
        return getOne(queryWrapper);
    }

    @Override
    public ReportSite getByReportSpaceId(Long reportSpaceId) {
        ReportSiteDTO query = new ReportSiteDTO();
        query.setReportSpaceId(reportSpaceId);
        return getReportSite(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReportSite saveReportSite(ReportSiteDTO reportSiteDTO) {
        log.info("saveReportSite - 保存报馆展位基本信息, reportSiteDTO={}", reportSiteDTO);
        ReportSite reportSite = BeanUtil.toBean(reportSiteDTO, ReportSite.class);
        save(reportSite);
        // 修改展商展位的单双层/是否吊点 信息
        updateExhibitorIsDoubleAndIsPoint(reportSite);
        return reportSite;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReportSite updateReportSite(ReportSiteDTO reportSiteDTO) {
        log.info("updateReportSite - 更新报馆展位基本信息, reportSiteDTO={}", reportSiteDTO);
        // 更新报馆展位基本信息
        ReportSite reportSite = BeanUtil.toBean(reportSiteDTO, ReportSite.class);
        updateById(reportSite);
        // 修改展商展位的单双层/是否吊点 信息
        updateExhibitorIsDoubleAndIsPoint(reportSite);
        return reportSite;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateExhibitorIsDoubleAndIsPoint(ReportSite reportSite) {
        // 修改展商展位的单双层/是否吊点 信息
        ReportSpace reportSpace = reportSpaceService.getById(reportSite.getReportSpaceId());
        if (reportSpace == null) {
            throw BizException.wrap(BuilderExceptionCode.REPORT_SPACE_NOT_EXIST);
        }
        ExhibitorDTO exhibitorDTO = new ExhibitorDTO();
        exhibitorDTO.setId(reportSpace.getExhibitorId());
        exhibitorDTO.setIsDouble(reportSite.getIsDouble());
        exhibitorDTO.setIsPoint(reportSite.getIsPoint());
        exhibitorService.updateIsDoubleAndIsPoint(exhibitorDTO);
        return true;
    }

    @Override
    public ReportSiteAuditDTO createAuditByReportSpaceId(Long reportSpaceId) {
        ReportSiteVO reportSite = getVOByReportSpaceId(reportSpaceId);
        if (reportSite == null) {
            throw BizException.wrap(BuilderExceptionCode.REPORT_SITE_NOT_EXIST.getCode(), "无法提交，请完善展位基本信息");
        }
        CopyOptions copyOptions = CopyOptions
                .create()
                .setIgnoreProperties(CommonConstants.COMMON_IGNORE_PROPERTIES);
        return BeanUtil.toBean(reportSite, ReportSiteAuditDTO.class, copyOptions);
    }

    @Override
    public boolean removeByReportSpaceId(Long reportSpaceId) {
        ReportSite reportSite = getByReportSpaceId(reportSpaceId);
        if (reportSite == null) {
            return false;
        }
        List<ReportSite> list = new ArrayList<>();
        list.add(reportSite);
        return removeByIdsOfLogic(list);
    }
}
